package model;

import ilog.concert.IloException;
import ilog.concert.IloLPMatrix;
import ilog.concert.IloNumVar;
import ilog.cplex.IloCplex;
import ilog.opl.IloOplDataSource;
import ilog.opl.IloOplErrorHandler;
import ilog.opl.IloOplException;
import ilog.opl.IloOplFactory;
import ilog.opl.IloOplModel;
import ilog.opl.IloOplModelDefinition;
import ilog.opl.IloOplModelSource;
import ilog.opl.IloOplSettings;

public class RandomFixing {

	static final String DATADIR = ".";
		   
	static final String MODEL_FILE = "knapsack";
	static final String MPS_FILE = "TSP";
	
    static public void main(String[] args) throws Exception
    {
      int status = 127;    
      try {
        IloOplFactory.setDebugMode(false);
        IloOplFactory oplF = new IloOplFactory();
        IloOplErrorHandler errHandler = oplF.createOplErrorHandler();
        IloOplModelSource modelSource = oplF.createOplModelSource(DATADIR
                + "/" + MODEL_FILE + ".mod");
        IloOplSettings settings = oplF.createOplSettings(errHandler);
        settings.setWithDecisionVariableNames(true);
        settings.setBigMapThreshold(1000);
        IloOplModelDefinition def = oplF.createOplModelDefinition(modelSource,settings);
        IloCplex cplex = oplF.createCplex();
        cplex.setOut(null);
        IloOplModel opl = oplF.createOplModel(def, cplex);
        IloOplDataSource dataSource = oplF.createOplDataSource(DATADIR
                + "/" + MODEL_FILE + ".dat");
        opl.addDataSource(dataSource);
        opl.generate();
        
        cplex.exportModel(DATADIR + "/" + MPS_FILE + ".mps");
        //naive cruncher 
        System.out.print("*************************\n\nFROM MPS FILE \n\n****************************\n");
        IloCplex newCplex = new IloCplex();
        
        printVarsFromMPSFile(newCplex,DATADIR + "/" + MPS_FILE + ".mps");
        
        newCplex.exportModel(DATADIR + "/" + MPS_FILE + "_added.mps");
        
        oplF.end();
		status = 0;
      } catch (IloOplException ex) {
        System.err.println("### OPL exception: " + ex.getMessage());
        ex.printStackTrace();
        status = 2;
      } catch (IloException ex) {
        System.err.println("### CONCERT exception: " + ex.getMessage());
        ex.printStackTrace();
        status = 3;
      } catch (Exception ex) {
        System.err.println("### UNEXPECTED UNKNOWN ERROR ...");
        ex.printStackTrace();
        status = 4;
      }
      System.exit(status);
    }

    static private void printVarsFromMPSFile(IloCplex cplex, String filename)
    {
    	try{
    	    cplex.importModel(filename);	
    	    printVars(cplex);
    	}
    	 catch (IloException e) {
    		    System.err.println("Concert exception caught: " + e);
    		    e.printStackTrace();
    		 }
    }
    
    
    static private void printVars(IloCplex cplex)
    {
    try{
    	cplex.solve();
       // Access the IloLPMatrix object that has been read from a file in
       // order to access variables which are the columns of the LP.  The
       // method importModel() guarantees that exactly one IloLPMatrix
       // object will exist, which is why no tests or iterators are
       // needed in the following line of code.
       IloLPMatrix lp = (IloLPMatrix)cplex.LPMatrixIterator().next();
       IloNumVar[] vars = lp.getNumVars();
       double[] vals = cplex.getValues(lp);
       
       for (int j = 0; j < vars.length; ++j)
          System.out.println("Variable " + vars[j].getName());
       
       int index = 5;
       double sol = 1; //vals[index];
       vars[index].setLB(sol);
       vars[index].setUB(sol);

 }
 catch (IloException e) {
    System.err.println("Concert exception caught: " + e);
    e.printStackTrace();
 }
}
}
